clear
set mem 2g
use psid-fertility

use birthrate, clear
gen stfips=1 if state=="AL"
replace stfips=2 if state=="AK"
replace stfips=4 if state=="AZ"
replace stfips=5 if state=="AR"
replace stfips=6 if state=="CA"
replace stfips=8 if state=="CO"
replace stfips=9 if state=="CT"
replace stfips=10 if state=="DE"
replace stfips=11 if state=="DC"
replace stfips=12 if state=="FL"
replace stfips=13 if state=="GA"
replace stfips=15 if state=="HI"
replace stfips=16 if state=="ID"
replace stfips=17 if state=="IL"
replace stfips=18 if state=="IN"
replace stfips=19 if state=="IA"
replace stfips=20 if state=="KS"
replace stfips=21 if state=="KY"
replace stfips=22 if state=="LA"
replace stfips=23 if state=="ME"
replace stfips=24 if state=="MD"
replace stfips=25 if state=="MA"
replace stfips=26 if state=="MI"
replace stfips=27 if state=="MN"
replace stfips=28 if state=="MS"
replace stfips=29 if state=="MO"
replace stfips=30 if state=="MT"
replace stfips=31 if state=="NE"
replace stfips=32 if state=="NV"
replace stfips=33 if state=="NH"
replace stfips=34 if state=="NJ"
replace stfips=35 if state=="NM"
replace stfips=36 if state=="NY"
replace stfips=37 if state=="NC"
replace stfips=38 if state=="ND"
replace stfips=39 if state=="OH"
replace stfips=40 if state=="OK"
replace stfips=41 if state=="OR"
replace stfips=42 if state=="PA"
replace stfips=44 if state=="RI"
replace stfips=45 if state=="SC"
replace stfips=46 if state=="SD"
replace stfips=47 if state=="TN"
replace stfips=48 if state=="TX"
replace stfips=49 if state=="UT"
replace stfips=50 if state=="VT"
replace stfips=51 if state=="VA"
replace stfips=53 if state=="WA"
replace stfips=54 if state=="WV"
replace stfips=55 if state=="WI"
replace stfips=56 if state=="WY"
gen rincpercap=rpcincome*cpi
collapse (mean) rincpercap unemprate, by(stfips year)
sort stfips year
merge stfips year using psid-fertility
tab _merge
drop if _merge~=3
drop _merge
gen lrincpercap=ln(rincpercap)

* Trim Sample *

replace asample=0 if tot_fam_y_lag0==9999999 & tot_fam_y_lag0~=.
replace asample=0 if tot_fam_y_lag0<-100000 & tot_fam_y_lag0~=.
replace asample=0 if (rhvaldiff_2<-30 | rhvaldiff_2>50) & rhvaldiff_2~=.
replace asample=0 if (rhvaldiff_4<-30 | rhvaldiff_4>150) & rhvaldiff_4~=.
replace asample=0 if (rhvaldiff_2<-30 | rhvaldiff_2>50) & rhvaldiff_2~=.
replace asample=0 if (housingpricemsadiff_4<-30 | housingpricemsadiff_4>150) & housingpricemsadiff_4~=. & ownhome==0
replace asample=0 if (housingpricemsadiff_2<-30 | housingpricemsadiff_2>50) & housingpricemsadiff_2~=. & ownhome==0

gen temp="-"
egen temp2=concat(in68 temp pn68)
replace uniqueid=temp2 if year<2007
drop temp temp2
egen uniqueid2=group(uniqueid)

egen flag=tag(uniqueid2 year)
keep if flag==1

xtset uniqueid2 year
gen birth_1 = l1.birth
gen msa_4=l4.msa
gen mover=1 if msa_4~=msa
replace mover=. if msa_4==.

* Look at Total Fertility within 5-year Bins *

xtset uniqueid2 year
*gen birth_1=l1.birth
gen birth_2=l2.birth
gen birth_3=l3.birth
gen birth_4=l4.birth
gen fouryrbirth=birth+birth_1+birth_2+birth_3

gen rhomeval_lag4=l4.rhomeval
gen own_lag4=l4.ownhome
gen rhomeval_lag8=l8.rhomeval
gen own_lag8=l8.ownhome

gen lagdiff_4=rhomeval_lag8-rhomeval_lag4
replace lagdiff_4=0 if own_lag8==0 | own_lag4==0

xi: reg birth rhvaldiff_4 lagdiff_4 own_lag8 own_lag4 married rtot_fam_y_lag0 fuchildren_lag0 age2024 age2529 age3034 age3539 age4044 hsdip somecoll ba edmiss unemp lrincpercap i.year i.msa if asample==1 & ownhome==1 & age>24 & mover~=1, cluster(msa_cluster)
xi: reg fouryrbirth rhvaldiff_4 lagdiff_4 own_lag8 own_lag4 married rtot_fam_y_lag0 fuchildren_lag0 age2024 age2529 age3034 age3539 age4044 hsdip somecoll ba edmiss unemp lrincpercap i.year i.msa if asample==1 & ownhome==1 & age>24 & mover~=1, cluster(msa_cluster)





